import { ref, onMounted } from 'vue'
import { getDicts } from '@/api/system/dict/data'

export function useDict(...args) {
  const res = ref({ type: {} })

  function getDictData() {
    return new Promise((resolve, reject) => {
      getDicts(args.join(',')).then(res => {
        if (res.code === 200) {
          const dict = {}
          res.data.forEach((d) => {
            dict[d.dictType] = d.data
          })
          res.value = { type: dict }
          resolve(dict)
        } else {
          reject(res)
        }
      })
    })
  }

  onMounted(() => {
    getDictData()
  })

  return {
    dict: res
  }
} 